﻿//https://leetcode.cn/problems/path-sum-iii/?envType=study-plan-v2&envId=top-100-liked

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    unordered_map<long long, int> prefix;
    int pathSum(TreeNode* root, int targetSum)
    {
        prefix[0] = 1;
        return dfs(root, 0, targetSum);
    }
    int dfs(TreeNode* root, long long cur, int targetSum)
    {
        if (root == nullptr)
            return 0;

        int ret = 0;
        cur += root->val;
        if (prefix.count(cur - targetSum))
        {
            ret += prefix[cur - targetSum];
        }

        prefix[cur]++;
        ret += dfs(root->left, cur, targetSum);
        ret += dfs(root->right, cur, targetSum);
        prefix[cur]--;

        return ret;
    }
};
